/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.WebIDE.ejb.sessions;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.ejb.Stateless;

/**
 *
 * @author Black Cat
 */
@Stateless
public class ProjectManagerBeanBean implements ProjectManagerBeanRemote, ProjectManagerBeanLocal {

    public File createFile(String strFileName) {
        File fileNew = new File(strFileName);
        return fileNew;
    }

    public boolean saveFile(String str, String strFileName) {        
        try{
            FileOutputStream outStream = new FileOutputStream(strFileName);
            DataOutputStream outDataStream = new DataOutputStream(outStream);
            outDataStream.writeUTF(str); 
            outDataStream.close();
            outStream.close();
        }
        catch(FileNotFoundException e){
            return false;
        }        
        catch(IOException e){            
            return false;
        }               
        return true;
    }

    public String readFile(String strFileName) {
        String s = null;
        
        try{
            FileInputStream inStream = new FileInputStream(strFileName);
            DataInputStream inDataStream = new DataInputStream(inStream);
            s = inDataStream.readUTF();
            inDataStream.close();
            inStream.close();
        }
        catch(FileNotFoundException e){            
        }
        catch(IOException e){            
        }        
        return s;
    }

    public String[] readProject(String strProjectName) {
        File directory = new File(strProjectName);
        String arrFileName[] = directory.list();
        return arrFileName;
    }

    public boolean deleteFile(String strFileName) {
        File f = new File(strFileName);
        if(f.delete())            
            return true;
        return false;
    }        

    public boolean deleteProject(File directory) {    
        if (directory.isDirectory()) {
            String[] children = directory.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteProject(new File(directory, children[i]));
                if (!success)
                    return false;                
            }
        }
        // The directory is now empty so delete it
        return directory.delete();
    }

    public File createProject(String strProjectName) {
        File directory = new File(strProjectName);
        
        return directory;
    }
    
    
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
 
}
